package com.hlbspringcloud.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * @Author huanglibin
 * @Project mq
 * @Description 路由模式 创建生产者 生产消息
 * @Date 2020-12-04-10-39-47
 * @Version 1.0
 */
public class RoutingSender {
    /**
     * 路由名称
     */
    public static final String ROUTING_NAME = "routing mode";

    /**
     * 发送消息
     * @throws Exception
     */
    public static void sendMessage() throws Exception{
        //1.创建连接
        Connection rabbitmqConnection = RabbitmqUntil.getRabbitmqConnection();
        //2.获取管道
        Channel channel = rabbitmqConnection.createChannel();
        //3、创建路由（参数1：路由名称；参数2：模式名称，direct表示路由模式，路由模式发布消息需要绑定routing-key）
        channel.exchangeDeclare(ROUTING_NAME,"direct");
        //4.设置每次发送消息不超过1条
        channel.basicQos(1);
        String message="hello ,I am error";
        String routingKey ="error";
        //5.发布消息（这里需要绑定routingKey）
        channel.basicPublish(ROUTING_NAME,routingKey,null,message.getBytes());
        message="hello ,I am waring";
        routingKey="warning";
        channel.basicPublish(ROUTING_NAME,routingKey,null,message.getBytes());
    }

    public static void main(String[] args) throws Exception {
        RoutingSender.sendMessage();
    }
}
